package com.audi.activity;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.GridView;
import android.widget.ImageView;
import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.ListHomeMemberAdapter;
import com.audi.dto.MemberItem;
import com.audi.handler.MemberHandlerAction;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;
import com.audi.utils.ProgressDialogUtil;

public class HomeMemberActivity extends BaseActivity {
	private ListHomeMemberAdapter mMemberAdapter;
	private ArrayList<MemberItem> mListMember;
	private GridView mListViewMember;
	private ImageView mHomeMemberDrawer;
	private SendMessage sendMessage;
	private ProgressDialog mProgressDialog;
	private MemberHandlerAction mMemberHandlerAction;

	protected void onCreate(Bundle bundle) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(bundle);
		setContentView(R.layout.activity_home_member);
		initActionBar();
		setListenerActionHomeMember();
		initView();
		Log.i("Client", "HomeMemberActivity.java sdafsadf");
		mListMember = new ArrayList<MemberItem>();
		sendMessage = new SendMessage();
		CreateProgressDialog(getString(R.string.loading));
		mMemberHandlerAction = new MemberHandlerAction(this);
		sendMessage.sendData(ActionMessage.getListMember, null,
				mMemberHandlerAction.loadListMemberHandler);
		DestroyProgressDialog();
	}

	private void initView() {
		tempId = R.layout.action_bar_main;
		mListViewMember = (GridView) findViewById(R.id.gr_member_list);

	}

	@Override
	public void onRefreshData() {
		mListMember = mMemberHandlerAction.getAllMember();
		if (mListMember != null) {
			mMemberAdapter = new ListHomeMemberAdapter(this, mListMember);
			mListViewMember.setAdapter(mMemberAdapter);
		} else {
			Log.e("--NULLL-----", "------------NULLL--------------");
		}
	}

	protected void initActionBar() {
		mHomeMemberDrawer = (ImageView) findViewById(R.id.btn_action_member_menu_more);
		mHomeMemberDrawer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
					mDrawerLayout.openDrawer(mDrawerList);
				} else {
					mDrawerLayout.closeDrawer(mDrawerList);
				}
			}
		});
	}

	public void CreateProgressDialog(String strMessage) {
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setTitle(null);
		mProgressDialog.setMessage(strMessage);
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setCancelable(true);
		mProgressDialog.show();
	}

	/**
	 * DestroyProgressDialog
	 * 
	 * @see destroy dialog
	 */
	public void DestroyProgressDialog() {
		if (mProgressDialog != null)
			mProgressDialog.dismiss();
	}

	public void setListenerActionHomeMember() {
		homeMember.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
					mDrawerLayout.openDrawer(mDrawerList);
				} else {
					mDrawerLayout.closeDrawer(mDrawerList);
				}
			}
		});
	}
}